package com.example.hybridmobiledemo.module.main.model.shop.classification.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.hybridmobiledemo.R;
import com.example.hybridmobiledemo.databinding.FragmentTissueBinding;
import com.example.hybridmobiledemo.module.main.model.shop.bean.Commodity;
import com.example.hybridmobiledemo.module.main.model.shop.bean.CommodityInfo;
import com.example.hybridmobiledemo.util.OkHttpUtils;
import com.example.hybridmobiledemo.view.ClaRViewAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ThermalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ThermalFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "id";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private RecyclerView rvCla;
    private TextView tvClaPrice, tvClaSalesVolume, tvClaDiscount;
    private String id;
    private List<Commodity> list = new ArrayList<>();
    private int mList = 0;
    private int vList = 0;
    private int dList = 0;

    private FragmentTissueBinding binding;

    public ThermalFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ThermalFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ThermalFragment newInstance(String param1, String param2, String id) {
        ThermalFragment fragment = new ThermalFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            id = getArguments().getString(ARG_PARAM3);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentTissueBinding.inflate(inflater, container, false);
        // Inflate the layout for this fragment
        initView();
        initData();
        return binding.getRoot();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.builder()
                        .url("interface/Commodity/getAll")
                        .addParam("userId", "ODky")
                        .get()
                        .async(new OkHttpUtils.AsyncCallBack() {
                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CommodityInfo commodity = gson.fromJson(json, CommodityInfo.class);
                                List<CommodityInfo.DataBean> Temp = commodity.getData();
                                for (int i = 0; i < Temp.size(); i++) {
                                    String mId = String.valueOf(Temp.get(i).getCommodityTypeId());
                                    if (mId.equals(id)) {
                                        list.add(new Commodity(Temp.get(i).getId(),
                                                Temp.get(i).getCommodityName(),
                                                id,
                                                Temp.get(i).getMoney(),
                                                Temp.get(i).getCostPrice(),
                                                Temp.get(i).getPic(),
                                                Temp.get(i).getVolume()));
                                    }
                                }
                                //                                刷新UI控件
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        rvCla.setLayoutManager(new LinearLayoutManager(getContext()));
                                        rvCla.setAdapter(new ClaRViewAdapter(list, getContext(), getActivity().getWindowManager()));
                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
            }
        }).start();
    }

    private void initView() {
        rvCla = binding.rvCla;
        tvClaPrice = binding.tvClaPrice;
        tvClaSalesVolume = binding.tvClaSalesVolume;
        tvClaDiscount = binding.tvClaDiscount;

        tvClaDiscount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvClaPrice.setTextColor(getActivity().getResources().getColor(R.color.black));
                tvClaSalesVolume.setTextColor(getActivity().getResources().getColor(R.color.black));
                tvClaDiscount.setTextColor(getActivity().getResources().getColor(R.color.red));
                Collections.sort(list, new Comparator<Commodity>() {
                    @Override
                    public int compare(Commodity c1, Commodity c2) {
                        float m1 = Float.parseFloat(c1.getMoney());
                        float m2 = Float.parseFloat(c2.getMoney());
                        float p1 = Float.parseFloat(c1.getCostPrice());
                        float p2 = Float.parseFloat(c2.getCostPrice());
                        float r1 = m1 / p1;
                        float r2 = m2 / p2;
                        if (dList == 0) {
                            dList = 1;
                            if (r1 > r2) {
                                return 1;
                            } else if (r1 < r2){
                                return -1;
                            }
                            return 0;
                        } else {
                            dList = 0;
                            if (r1 > r2) {
                                return -1;
                            } else if (r1 < r2){
                                return 1;
                            }
                        }
                        return 0;
                    }
                });
                rvCla.setLayoutManager(new LinearLayoutManager(getContext()));
                rvCla.setAdapter(new ClaRViewAdapter(list, getContext(), getActivity().getWindowManager()));
            }
        });

        tvClaPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvClaPrice.setTextColor(getActivity().getResources().getColor(R.color.red));
                tvClaSalesVolume.setTextColor(getActivity().getResources().getColor(R.color.black));
                tvClaDiscount.setTextColor(getActivity().getResources().getColor(R.color.black));
                if (mList == 0) {
                    mList = 1;
                    Collections.sort(list, new Comparator<Commodity>() {
                        @Override
                        public int compare(Commodity c1, Commodity c2) {
                            double m1 = Double.parseDouble(c1.getMoney());
                            double m2 = Double.parseDouble(c2.getMoney());
                            if (m1 > m2) {
                                return 1;
                            } else if (m1 < m2){
                                return -1;
                            }
                            return 0;
                        }
                    });
                } else {
                    mList = 0;
                    Collections.sort(list, new Comparator<Commodity>() {
                        @Override
                        public int compare(Commodity c1, Commodity c2) {
                            double m1 = Double.parseDouble(c1.getMoney());
                            double m2 = Double.parseDouble(c2.getMoney());
                            if (m1 > m2) {
                                return -1;
                            } else if (m1 < m2){
                                return 1;
                            }
                            return 0;
                        }
                    });
                }
                rvCla.setLayoutManager(new LinearLayoutManager(getContext()));
                rvCla.setAdapter(new ClaRViewAdapter(list, getContext(), getActivity().getWindowManager()));
            }
        });
        tvClaSalesVolume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvClaPrice.setTextColor(getActivity().getResources().getColor(R.color.black));
                tvClaSalesVolume.setTextColor(getActivity().getResources().getColor(R.color.red));
                tvClaDiscount.setTextColor(getActivity().getResources().getColor(R.color.black));
                if (vList == 0) {
                    vList = 1;
                    Collections.sort(list, new Comparator<Commodity>() {
                        @Override
                        public int compare(Commodity c1, Commodity c2) {
                            double v1 = Double.parseDouble(c1.getVolume());
                            double v2 = Double.parseDouble(c2.getVolume());
                            if (v1 > v2) {
                                return 1;
                            } else if (v1 < v2){
                                return -1;
                            }
                            return 0;
                        }
                    });
                } else {
                    vList = 0;
                    Collections.sort(list, new Comparator<Commodity>() {
                        @Override
                        public int compare(Commodity c1, Commodity c2) {
                            double v1 = Double.parseDouble(c1.getVolume());
                            double v2 = Double.parseDouble(c2.getVolume());
                            if (v1 > v2) {
                                return -1;
                            } else if (v1 < v2){
                                return 1;
                            }
                            return 0;
                        }
                    });
                }
                rvCla.setLayoutManager(new LinearLayoutManager(getContext()));
                rvCla.setAdapter(new ClaRViewAdapter(list, getContext(), getActivity().getWindowManager()));
            }
        });
    }
}